/*
 * @Author: 前端一理 1041123705@qq.com
 * @Date: 2023-11-22 14:57:43
 * @LastEditors: 前端一理 1041123705@qq.com
 * @LastEditTime: 2023-11-23 16:36:06
 * @FilePath: \react\src\api\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";

const api = axios.create({
  baseURL: "http://116.205.240.12:5700/api",
});

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return response.data;
    }

    return Promise.reject(response);
  },
  (error) => {
    if (error.response.status === 401) {
      sessionStorage.removeItem("token");
      return error.response.data;
    }
    return Promise.reject(error);
  }
);
// 请求拦截器 添加token
api.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem("token");
    if (token) {
      config.headers["Authorization"] = "Bearer " + token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 获取当时间搓
const getTimestamp = () => {
  return new Date().getTime();
};

// 登录
export const login = (payload) =>
  api.post(`/user/login?t=${getTimestamp()}`, payload);

// 获取key列表
export const getKeyList = (payload) => {
  return api.get(`/envs?t=${getTimestamp()}`, { searchValue: payload });
};
